package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.City;
import com.xj.edu.hotel.domain.Hotel;
import com.xj.edu.hotel.domain.Room;
import com.xj.edu.hotel.service.CityService;
import com.xj.edu.hotel.service.HotelService;
import com.xj.edu.hotel.service.OrderService;
import com.xj.edu.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;
    @Autowired
    private CityService cityService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomService roomService;
    /**
     * 酒店信息查询
     */
    @RequestMapping("findAllHotel")
    public String findAllHotel(Model model){
        List<Hotel> list = hotelService.findAllHotel();
        model.addAttribute("hotel",list);
        return "hotel";
    }

    /**
     * 查询所有酒店的名称
     */
    @RequestMapping("findAllHotelName")
    @ResponseBody
    public List<Hotel> findAllHotelName(String cityId){
        List<Hotel> list = hotelService.findAllHotelName(cityId);
        return list;
    }
    /**
     * 酒店信息删除
     */
    @RequestMapping("delHotel")
    public void delHotel(String id){
        hotelService.deleteHotel(id);
    }
    /**
     * 酒店信息添加
     */
    @RequestMapping(value = "addHotel",method = RequestMethod.POST)
    public String addHotel(Hotel hotel, @RequestParam(value = "file") MultipartFile file, String cityName) throws IOException {

        if(file.getOriginalFilename() != null){
            try {
                String filename = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                hotel.setFileName(filename);
                hotel.setFileContentType(file.getContentType());
                hotel.setHotePicPath("D:/myfile");
                Files.write(Paths.get("D:","myfile",filename),file.getBytes());
            }catch (Exception e){
               e.getStackTrace();
            }

        }

        City city = cityService.selectCityByName(cityName);
        hotel.setCityId(city.getCityId());
        hotelService.addHotel(hotel);
        return "redirect:/hotel/findAllHotel";
    }
    /**
     * 酒店信息修改
     */
    @RequestMapping("editHotel")
    public String editHotel(Hotel hotel,String cityName,@RequestParam("file") MultipartFile file) throws IOException {
        String filename = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if(file != null){
            hotel.setFileName(filename);
            hotel.setFileContentType(file.getContentType());
            hotel.setHotePicPath("D:/myfile");
        }
        City city = cityService.selectCityByName(cityName);
        hotel.setCityId(city.getCityId());
       int success = hotelService.editHotel(hotel);
        Files.write(Paths.get("D:","myfile",filename),file.getBytes());
       return "redirect:/hotel/findAllHotel";

    }

    /**
     * 通过id查找
     */
    @RequestMapping("findById")
    public String findById(String id,Model model){
       Hotel hotel = hotelService.findById(id);
       model.addAttribute("hotel",hotel);
       return "editHotelForm";
    }

    /**
     * 酒店房间数据统计
     */
    @RequestMapping("hotelRoomData")
    public String hotelRoomData(String cityId, String hotelId,Model model){
        List<Hotel> hotels = hotelService.hotelRoom(cityId,hotelId);
        model.addAttribute("hotels",hotels);
        return "hotelRoom";
    }
    /**
     * 查询所有的酒店类型
     */
    @RequestMapping("findHotelType")
    @ResponseBody
    public List<Hotel> findHotelType(){
       List<Hotel> list = hotelService.findAllHotel();
       return list;


    }

    /**
     * 为每个酒店的每类房间上传图片
     */
    @RequestMapping("uploadRoomPic")
    @ResponseBody
    public String uploadRoomPic(String id, @RequestParam("file") MultipartFile file) throws IOException{
       Room room = roomService.findRoomByRoomId(id);
       String filename = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
       if(file!=null){
           room.setRoomPicname(filename);
       }
        int success = roomService.updateRoomPic(room);
       if (success > 0){
           Files.write(Paths.get("D:","/myfile/room",filename),file.getBytes());
           return "ok";
       }else {
           return "error";
       }
    }
}
